<?php

class FormBuilder {
	
	var $promotion;
	
	// constructor
	function FormBuilder(){
	
	
	}
	
	
	
	
	
	// generate default order form
	// arg:$formelements array	array of form elements
	// arg:$target string	page that form goes to
	function buildDefaultOrderForm($promotion, $target){
	$this->promotion = $promotion;
		echo ("<div class=\"formBlock\">");
		echo ('<form action="'.$target.'" method="post" id="quantityForm" onsubmit="return confirmSubmit()">'."\n");
		
		$html = '<input name="promo" type="hidden" id="promo" value="'.$_GET['promo'].'" />'."\n";
		$html .= '<input name="image" type="hidden" id="image" value="'.$_GET['image'].'" />'."\n";
		
		
		if ($promotion->daysArray != NULL){
			echo($this->writeDays($promotion->daysArray));
		}
		

		$banners = $promotion->banners;
		$pointOfSale = $promotion->pointOfSale;
		$posters = $promotion->posters;
		$tableTalkers = $promotion->tableTalkers;
		$others = $promotion->others;
		
		$html .= "<table id=\"choosequantitiestable\">";
		if(count($banners)>0){
			$html .= $this->buildSection($banners,"Banners", 1);
		}
		if(count($pointOfSale)>0){
			$html .= $this->buildSection($pointOfSale, "POS", count($banners)+1);
		}
		if(count($posters)>0){
			$html .= $this->buildSection($posters, "Posters", count($pointOfSale)+count($banners)+1);
		}
		if(count($tableTalkers)>0){
			$html .= $this->buildSection($tableTalkers, "Table Talkers", count($pointOfSale)+count($banners)+count($posters)+1);
		}
		if(count($others)>0){
			$html .= $this->buildSection($others, "Others", count($tableTalkers)+1);
		}
		
		$html .= "<tr><th colspan=\"3\">Sub Total</th><th>SUB TOTAL</th></tr>\n";
		$html .= "<tr>\n";
		
		$html .= "<td><label>Sub Total</label></td>\n";
		$html .= "<td>&nbsp;</td>\n";
		$html .= "<td>&nbsp;</td>\n";
		$html .= '<td><input name="subtotal" type="text" id="subtotal" value="0" size="6" maxlength="6"/></td>'."\n";
		$html .= "</tr>\n";
		
		$html .= "<tr><th colspan=\"4\">Delivery</th></tr>\n";
		$html .= "<tr>\n";
		$html .= "<td>Delivery</td>\n";
		$html .= "<td>1 tube to 1 address</td>\n";
		$html .= "<td>&nbsp;</td>\n";
		$html .= "<td><p>&pound;8.25</p></td>\n";
		$html .= "</tr>\n";
		
		$html .= "<tr>\n";
		$html .= "<td>Please note delivery charge of Table talker holders is separate to POS orders due to weight &amp; bulk</td>\n";
		$html .= "<td></td>\n";
		$html .= "<td>&nbsp;</td>\n";
		$html .= "<td><p>&pound;9.25</p></td>\n";
		$html .= "</tr>\n";
		
		$html .= "<tr>\n";
		$html .= "<td colspan=\"4\"><span style=\"font-style:italic;\">NB. Where possible, multiple orders to a single destination and orders made on the same day will incur a single delivery charge.
Costs per unit are for print and delivery.</span><br/>
All creative costs are covered by Aramark head office.<br/>
<span style=\"font-style:italic;\">A &pound;15.00 build fee will be billed as a Central Charge to Aramark.</span></td>\n";
		$html .= "</tr>\n";
		
		$html .= "<tr><th colspan=\"3\">Total</th><th>TOTAL</th></tr>\n";
		$html .= "<tr>\n";
		$html .= "<td>&nbsp;</td>\n";
		$html .= "<td>&nbsp;</td>\n";
		$html .= "<td>&nbsp;</td>\n";
		$html .= "<td><input name=\"total\" type=\"text\" id=\"total\" value=\"0\" size=\"6\" maxlength=\"6\"/></td>\n";
		$html .= "</tr>\n";

		
		$html .= "<tr><td colspan=\"4\" style=\"text-align:right;\"><input name=\"Calculate\" type=\"button\" id=\"Calculate\" value=\"Calculate\" onclick=\"createTotal()\" style=\"margin:10px 0;\" /></td></tr>";

		$html .= "<tr>\n";
		$html .= "<td colspan=\"4\">If this is the poster you want, please press continue. Or press back to select a different image.</td>\n";
		$html .= "</tr>\n";
		
		$html .= "<tr>\n";
		$html .= "<td colspan=\"2\">Delivery Address</td>\n";
		$html .= "<td colspan=\"2\">".nl2br($_SESSION['address'])."</td>\n";
		$html .= "</tr>\n";




		$html .= "<tr><td colspan=\"4\" style=\"text-align:right;\"> <input name=\"Continue\" type=\"submit\" id=\"Continue\" value=\"Continue\"/></td></tr>";

		$html .= "</table>";
		
		$html .= '</form>'."\n";
		$html .= "</div>\n";
		return $html;
	
	}
	
	function buildJavascript($promotion){
	$html = "";
	$html .="<script type=\"text/javascript\">\n";
	$html .="<!--\n";
	$html .="function createTotal(){\n";
		$banners = $promotion->banners;
		$pointOfSale = $promotion->pointOfSale;
		$posters = $promotion->posters;
		$tableTalkers = $promotion->tableTalkers;
		$others = $promotion->others;
		
		
		if(count($banners)>0){
			$html .= $this->buildJavascriptSection($banners);
		}
		if(count($pointOfSale)>0){
			$html .= $this->buildJavascriptSection($pointOfSale);
		}
		if(count($posters)>0){
			$html .= $this->buildJavascriptSection($posters);
		}
		if(count($tableTalkers)>0){
			$html .= $this->buildJavascriptSection($tableTalkers);
		}
		if(count($others)>0){
			$html .= $this->buildJavascriptSection($others);
		}
		
	$subTotalString = "var subTotal = ";
	if(count($banners)>0){
		$subTotalString .= $this->buildJavascriptSubTotalString($banners);
	}
	if(count($pointOfSale)>0){
		$subTotalString .= $this->buildJavascriptSubTotalString($pointOfSale);
	}
	if(count($posters)>0){
		$subTotalString .= $this->buildJavascriptSubTotalString($posters);
	}
	if(count($tableTalkers)>0){
		$subTotalString .= $this->buildJavascriptSubTotalString($tableTalkers);
	}
	if(count($others)>0){
		$subTotalString .= $this->buildJavascriptSubTotalString($others);
	}
	
	$subTotalString = substr_replace($subTotalString ,"",-1).";";
	
	
	$html .= $subTotalString."\n";
	
	$html .= "document.getElementById(\"subtotal\").value = subTotal;";
	
	// section to add up the total = subtotal + delivery
	$html .= "if (document.getElementById('Table_Talker_Acrylic')){\n";
		$html .= "if (document.getElementById('Table_Talker_Acrylic').value > 0){\n";
		$html .= "var deliveryCost = 9.25;\n";
		$html .= "}else{\n";
		$html .= "var deliveryCost = 8.25;\n";
		$html .= "}\n";
	$html .= "}else{\n";
		$html .= "var deliveryCost = 8.25;\n";
	$html .= "}\n";	
	
	
	$html .= "document.getElementById('total').value = formatTotal(subTotal+deliveryCost);";
	
	$html .="}\n";
	
	
	
	$html .= "function formatTotal(total){\n";
		$html .= "var theTotal = total.toString();\n";
		$html .= "if (theTotal.indexOf(\".\") < 0) {\n";
			$html .= "total = total+'.00';\n";
			$html .= "return total; //return the whole integer\n";
			$html .= " }else{\n";
			$html .= "var endArray = theTotal.split('.'); //grab the end part\n";
			$html .= "var theBack = endArray[1];\n";
				$html .= "if (theBack.length == 2){\n";
				$html .= "return theTotal\n";
				$html .= "}else if(theBack.length > 2) {\n";
	$html .= "theBack = theBack.substring(0, 2);\n";
	$html .= "theTotal = endArray[0]+'.'+theBack;\n";
	$html .= "return theTotal;\n";
	$html .= "}else{\n";
	$html .= "theTotal = endArray[0]+'.'+theBack+'0';\n";
	$html .= "return theTotal;\n";
	$html .= "	}\n";
	$html .= "}\n";
	$html .= "}\n";
	
	
	
	
	$html .= "function confirmSubmit(){\n";
	
	$html .= "createTotal();\n";
	
	$html .= "if (document.getElementById('subtotal').value == 0 ){\n";
		$html .= "alert (\"You have selected no items.\");\n";
		$html .= "return false;\n";
		$html .= "}else{\n";
		$html .= "var answer = confirm(\"Are you sure you want to continue?\");\n";
	$html .= "if (answer){\n";
	$html .= "return true;\n";
	$html .= "}else{\n";
	$html .= "return false;\n";
	$html .= "}\n";
	$html .= "}\n";
		
	
			
			
	$html .= "}\n";
	
	$html .="-->\n";
	$html .="</script>\n";
	return $html;
	}
	
	function buildSection($section,$header, $j){
		$html = "";
		$promotionalItems = $section;
		$numberOfPromoItems = count($promotionalItems);
		$html .= "<tr><th>".$header."</th><th>QTY</th><th>COST</th><th>SUB TOTAL</th></tr>";
		
		for ($i=0; $i < $numberOfPromoItems; $i++) {
			$html .= '<tr>';
			$html .= '<td><label>'.$promotionalItems[$i]->itemName.'</label></td>'."\n";
			$html .= '<td><input tabindex="'.$j.'" name="'.$this->formatIdAndNameValues($promotionalItems[$i]->itemName).'" type="text" id="'.$this->formatIdAndNameValues($promotionalItems[$i]->itemName).'" value="0" size="2" maxlength="5"/></td>'."\n";
			$html .= '<td><p>&pound;'.$this->formatPrice($promotionalItems[$i]->itemCost)."</p></td>\n";
			$j++;
			$html .= '<td><input tabindex="'.($j+20).'" name="price_'.$this->formatIdAndNameValues($promotionalItems[$i]->itemName).'" type="text" id="price_'.$this->formatIdAndNameValues($promotionalItems[$i]->itemName).'" value="0" size="4" maxlength="6"/></td>'."\n";
			$html .= '</tr>';
		}
		
		return $html;
	}
	function buildJavascriptSubTotalString($section){
		$numberOfPromoItems = count($section);
		$subTotalString = "";
			for ($i=0; $i < $numberOfPromoItems; $i++) {
				$subTotalString .= $this->formatIdAndNameValues($section[$i]->itemName)."+";
			}
		return $subTotalString;
	}
	
	function buildJavascriptSection($section){
		$promotionalItems = $section;
		$numberOfPromoItems = count($promotionalItems);
			// 	var a0 = document.getElementById('a0').value*19.50;
		for ($i=0; $i < $numberOfPromoItems; $i++) {
			$html .= "var ".$this->formatIdAndNameValues($promotionalItems[$i]->itemName)." = document.getElementById('".$this->formatIdAndNameValues($promotionalItems[$i]->itemName)."').value*".$promotionalItems[$i]->itemCost.";\n";
			$html .= "document.getElementById('price_".$this->formatIdAndNameValues($promotionalItems[$i]->itemName)."').value = ".$this->formatIdAndNameValues($promotionalItems[$i]->itemName).";\n";
		
		}
		return $html;
	}
	
	// function to delete spaces and close up phrases
	// for form elements
	function formatIdAndNameValues($phrase){
		$str = str_replace  ( " "  , "_"  , $phrase );
		return $str;
	}
	function removeFormatIdAndNameValues($phrase){
		$str = str_replace  ( "_"  , " "  , $phrase );
		return $str;
	}
	
	function writeDays($days){
	
	$html ="<h2>Select Day</h2>";
	$html .= "<select name=\"days\">";
	foreach($days as $key => $val){
	$html .= "<option value=\"".$val."\">".$val."</option>";
	}
	$html .= "</select>";
	return $html;
}

	function formatPrice($price){
	// $this->formatPrice()
		if (substr_count($price,".") < 1) {
		$price = $price.".00";
		return $price;
	}else{
	$endArray = explode(".", $price);
	$theBack = $endArray[1];
		if (strlen($endArray[1]) == 2){
		return $price;
		}else if( strlen($theBack) > 2){
		$theBack = substr($theBack,0, 2);
		$price = $endArray[0].".".$theBack;
		return $price;
		}else{
		$price = $endArray[0].".".$theBack."0";
		return $price;
		}
	}	
}
function buildDefaultConfirmForm($post, $promotion){
	$this->promotion = $promotion;
		$html .= ("<div class=\"formBlock\">");
		$html .= "<h1>".$nameOfPromotion."</h1>\n";
		
		if ($post['days']) { $html .= "<p>Days: ".$post['days']."</p>";}
		
		$banners = $promotion->banners;
		$pointOfSale = $promotion->pointOfSale;
		$posters = $promotion->posters;
		$tableTalkers = $promotion->tableTalkers;
		$others = $promotion->others;
		
		$html .= "<table id=\"testTable\">";
		if(count($banners)>0){
			$html .= $this->buildConfirmSection($banners,"Banners", $post);
		}
		if(count($pointOfSale)>0){
			$html .= $this->buildConfirmSection($pointOfSale, "POS", $post);
		}
		if(count($posters)>0){
			$html .= $this->buildConfirmSection($posters, "Posters", $post);
		}
		if(count($tableTalkers)>0){
			$html .= $this->buildConfirmSection($tableTalkers, "Table Talkers", $post);
		}
		if(count($others)>0){
			$html .= $this->buildConfirmSection($others, "Others", $post);
		}
	//	$html .= "</table>";
	//	$html .= "<table id=\"\">";
		$html .= "<tr><th colspan=\"2\">Totals</th></tr>";
		$html .= "<tr>\n";
		$html .= "<td>Sub Total</td>\n";
		$html .= '<td>&pound;'.$post['subtotal']."</td>\n";
		$html .= "</tr>\n";
		$html .= "<tr>\n";
		$html .= "<td>Delivery</td>\n";
		$html .= '<td>&pound;'.($post['total']-$post['subtotal'])."</td>\n";
		$html .= "</tr>\n";
		$html .= "<tr>\n";
		$html .= "<td>Total</td>\n";
		$html .= '<td>&pound;'.$post['total']."</td>\n";
		$html .= "</tr>\n";
		$html .= "</table>";
		$html .= "</div>\n";
		return $html;
	
	}
function buildConfirmSection($section, $header, $post){
		
		$promotionalItems = $section;
		$numberOfPromoItems = count($promotionalItems);
		$html .= "<tr><th>".$header."</th><th>QTY</th><th>COST</th><th>SUB TOTAL</th></tr>";
		
		for ($i=0; $i < $numberOfPromoItems; $i++) {
			$html .= '<tr';
			
			if ( $post[$this->formatIdAndNameValues($promotionalItems[$i]->itemName)] > 0) {
				$html .= " class=\"hilite\" ";
			}
			
			$html .= '>';
			
			$html .= '<td>'.$promotionalItems[$i]->itemName.'</td>'."\n";
			$html .= '<td>'.$post[$this->formatIdAndNameValues($promotionalItems[$i]->itemName)].'</td>'."\n";
			$html .= '<td>&pound;'.$this->formatPrice($post["price_".$this->formatIdAndNameValues($promotionalItems[$i]->itemName)])."</td>\n";
			$html .= '<td>&pound;'.$this->formatPrice($post[$this->formatIdAndNameValues($promotionalItems[$i]->itemName)] * $post["price_".$this->formatIdAndNameValues($promotionalItems[$i]->itemName)])."</td>\n";
			$html .= '</tr>';
		}
		
		return $html;

	}
}
?>
